package org.mat.ipaas.ucenter.bff.admin.modules.system;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.mat.framework.lang.dto.MatApiResponse;
import org.mat.ipaas.ucenter.service.api.system.SysPermissionRestAPI;
import org.mat.ipaas.ucenter.service.constant.CommonConstant;
import org.mat.ipaas.ucenter.service.dto.system.entity.SysPermissionDTO;
import org.mat.ipaas.ucenter.service.dto.system.entity.SysPermissionDataRuleDTO;
import org.mat.ipaas.ucenter.service.dto.system.model.SysPermissionTreeDTO;
import org.mat.ipaas.ucenter.service.dto.system.model.TreeModelDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @Author scott
 * @since 2018-12-21
 */
@Slf4j
@RestController
@RequestMapping("/sys/permission")
public class SysPermissionController {

	@Autowired
	private SysPermissionRestAPI sysPermissionRestAPI;

	/**
	 * 加载数据节点
	 *
	 * @return
	 */
	//@RequiresPermissions("system:permission:list")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public MatApiResponse<List<SysPermissionTreeDTO>> list(SysPermissionDTO sysPermissionDTO) {
		MatApiResponse<List<SysPermissionTreeDTO>> matApiResponse = sysPermissionRestAPI.list(sysPermissionDTO);
		return matApiResponse;
	}

	/**
	 * 系统菜单列表(一级菜单)
	 *
	 * @return
	 */
	@RequestMapping(value = "/getSystemMenuList", method = RequestMethod.GET)
	public MatApiResponse<List<SysPermissionTreeDTO>> getSystemMenuList() {
		MatApiResponse<List<SysPermissionTreeDTO>> matApiResponse = sysPermissionRestAPI.getSystemMenuList();
		return matApiResponse;
	}

	/**
	 * 查询子菜单
	 * @param parentId
	 * @return
	 */
	@RequestMapping(value = "/getSystemSubmenu", method = RequestMethod.GET)
	public MatApiResponse<List<SysPermissionTreeDTO>> getSystemSubmenu(@RequestParam("parentId") String parentId){
		MatApiResponse<List<SysPermissionTreeDTO>> matApiResponse = sysPermissionRestAPI.getSystemSubmenu(parentId);
		return matApiResponse;
	}

	/**
	 * 查询子菜单
	 *
	 * @param parentIds 父ID（多个采用半角逗号分割）
	 * @return 返回 key-value 的 Map
	 */
	@GetMapping("/getSystemSubmenuBatch")
	public MatApiResponse getSystemSubmenuBatch(@RequestParam("parentIds") String parentIds) {
		MatApiResponse matApiResponse = sysPermissionRestAPI.getSystemSubmenuBatch(parentIds);
		return matApiResponse;
	}

	/**
	 * 查询用户拥有的菜单权限和按钮权限
	 *
	 * @return
	 */
	@RequestMapping(value = "/getUserPermissionByToken", method = RequestMethod.GET)
	//@DynamicTable(value = DynamicTableConstant.SYS_ROLE_INDEX)
	public MatApiResponse<?> getUserPermissionByToken(HttpServletRequest request) {
		String version = request.getHeader(CommonConstant.VERSION);
		MatApiResponse<?> matApiResponse = sysPermissionRestAPI.getUserPermissionByToken(version);
		return matApiResponse;
	}

	/**
	 * 【vue3专用】获取
	 * 1、查询用户拥有的按钮/表单访问权限
	 * 2、所有权限 (菜单权限配置)
	 * 3、系统安全模式 (开启则online报表的数据源必填)
	 */
	@RequestMapping(value = "/getPermCode", method = RequestMethod.GET)
	public MatApiResponse<?> getPermCode() {
		MatApiResponse<?> matApiResponse = sysPermissionRestAPI.getPermCode();
		return matApiResponse;
	}

	/**
	  * 添加菜单
	 * @param permission
	 * @return
	 */
    //@RequiresPermissions("system:permission:add")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public MatApiResponse<SysPermissionDTO> add(@RequestBody SysPermissionDTO permission) {
		MatApiResponse<SysPermissionDTO> matApiResponse = sysPermissionRestAPI.add(permission);
		return matApiResponse;
	}

	/**
	  * 编辑菜单
	 * @param permission
	 * @return
	 */
    //@RequiresPermissions("system:permission:edit")
	@RequestMapping(value = "/edit", method = { RequestMethod.PUT, RequestMethod.POST })
	public MatApiResponse<SysPermissionDTO> edit(@RequestBody SysPermissionDTO permission) {
		MatApiResponse<SysPermissionDTO> matApiResponse = sysPermissionRestAPI.edit(permission);
		return matApiResponse;
	}

	/**
	 * 检测菜单路径是否存在
	 * @param id
	 * @param url
	 * @return
	 */
	@RequestMapping(value = "/checkPermDuplication", method = RequestMethod.GET)
	public MatApiResponse<String> checkPermDuplication(@RequestParam(name = "id", required = false) String id, @RequestParam(name = "url") String url, @RequestParam(name = "alwaysShow") Boolean alwaysShow) {
		MatApiResponse<String> matApiResponse = sysPermissionRestAPI.checkPermDuplication(id, url, alwaysShow);
		return matApiResponse;
	}

	/**
	  * 删除菜单
	 * @param id
	 * @return
	 */
    //@RequiresPermissions("system:permission:delete")
	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
	public MatApiResponse<SysPermissionDTO> delete(@RequestParam(name = "id", required = true) String id) {
		MatApiResponse<SysPermissionDTO> matApiResponse = sysPermissionRestAPI.delete(id);
		return matApiResponse;
	}

	/**
	  * 批量删除菜单
	 * @param ids
	 * @return
	 */
    //@RequiresPermissions("system:permission:deleteBatch")
	@RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
	public MatApiResponse<SysPermissionDTO> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		MatApiResponse<SysPermissionDTO> matApiResponse = sysPermissionRestAPI.deleteBatch(ids);
		return matApiResponse;
	}

	/**
	 * 获取全部的权限树
	 *
	 * @return
	 */
	@RequestMapping(value = "/queryTreeList", method = RequestMethod.GET)
	public MatApiResponse<Map<String, Object>> queryTreeList() {
		MatApiResponse<Map<String, Object>> matApiResponse = sysPermissionRestAPI.queryTreeList();
		return matApiResponse;
	}

	/**
	 * 异步加载数据节点 [接口是废的,没有用到]
	 *
	 * @return
	 */
	@RequestMapping(value = "/queryListAsync", method = RequestMethod.GET)
	public MatApiResponse<List<TreeModelDTO>> queryAsync(@RequestParam(name = "pid", required = false) String parentId) {
		MatApiResponse<List<TreeModelDTO>> matApiResponse = sysPermissionRestAPI.queryAsync(parentId);
		return matApiResponse;
	}

	/**
	 * 查询角色授权
	 *
	 * @return
	 */
	@RequestMapping(value = "/queryRolePermission", method = RequestMethod.GET)
	public MatApiResponse<List<String>> queryRolePermission(@RequestParam(name = "roleId", required = true) String roleId) {
		MatApiResponse<List<String>> matApiResponse = sysPermissionRestAPI.queryRolePermission(roleId);
		return matApiResponse;
	}

	/**
	 * 保存角色授权
	 *
	 * @return
	 */
    //@RequiresPermissions("system:permission:saveRole")
	@RequestMapping(value = "/saveRolePermission", method = RequestMethod.POST)
	public MatApiResponse<String> saveRolePermission(@RequestBody JSONObject json) {
		MatApiResponse<String> matApiResponse = sysPermissionRestAPI.saveRolePermission(json);
		return matApiResponse;
	}

	/**
	 * 根据菜单id来获取其对应的权限数据
	 *
	 * @param sysPermissionDataRuleDTO
	 * @return
	 */
	@RequestMapping(value = "/getPermRuleListByPermId", method = RequestMethod.GET)
	public MatApiResponse<List<SysPermissionDataRuleDTO>> getPermRuleListByPermId(SysPermissionDataRuleDTO sysPermissionDataRuleDTO) {
		MatApiResponse<List<SysPermissionDataRuleDTO>> matApiResponse = sysPermissionRestAPI.getPermRuleListByPermId(sysPermissionDataRuleDTO);
		return matApiResponse;
	}

	/**
	 * 添加菜单权限数据
	 *
	 * @param sysPermissionDataRuleDTO
	 * @return
	 */
    //@RequiresPermissions("system:permission:addRule")
	@RequestMapping(value = "/addPermissionRule", method = RequestMethod.POST)
	public MatApiResponse<SysPermissionDataRuleDTO> addPermissionRule(@RequestBody SysPermissionDataRuleDTO sysPermissionDataRuleDTO) {
		MatApiResponse<SysPermissionDataRuleDTO> matApiResponse = sysPermissionRestAPI.addPermissionRule(sysPermissionDataRuleDTO);
		return matApiResponse;
	}

    //@RequiresPermissions("system:permission:editRule")
	@RequestMapping(value = "/editPermissionRule", method = { RequestMethod.PUT, RequestMethod.POST })
	public MatApiResponse<SysPermissionDataRuleDTO> editPermissionRule(@RequestBody SysPermissionDataRuleDTO sysPermissionDataRuleDTO) {
		MatApiResponse<SysPermissionDataRuleDTO> matApiResponse = sysPermissionRestAPI.editPermissionRule(sysPermissionDataRuleDTO);
		return matApiResponse;
	}

	/**
	 * 删除菜单权限数据
	 *
	 * @param id
	 * @return
	 */
    //@RequiresPermissions("system:permission:deleteRule")
	@RequestMapping(value = "/deletePermissionRule", method = RequestMethod.DELETE)
	public MatApiResponse<SysPermissionDataRuleDTO> deletePermissionRule(@RequestParam(name = "id", required = true) String id) {
		MatApiResponse<SysPermissionDataRuleDTO> matApiResponse = sysPermissionRestAPI.deletePermissionRule(id);
		return matApiResponse;
	}

	/**
	 * 查询菜单权限数据
	 *
	 * @param sysPermissionDataRuleDTO
	 * @return
	 */
	@RequestMapping(value = "/queryPermissionRule", method = RequestMethod.GET)
	public MatApiResponse<List<SysPermissionDataRuleDTO>> queryPermissionRule(SysPermissionDataRuleDTO sysPermissionDataRuleDTO) {
		MatApiResponse<List<SysPermissionDataRuleDTO>> matApiResponse = sysPermissionRestAPI.queryPermissionRule(sysPermissionDataRuleDTO);
		return matApiResponse;
	}

	/**
	 * 部门权限表
	 * @param departId
	 * @return
	 */
	@RequestMapping(value = "/queryDepartPermission", method = RequestMethod.GET)
	public MatApiResponse<List<String>> queryDepartPermission(@RequestParam(name = "departId", required = true) String departId) {
		MatApiResponse<List<String>> matApiResponse = sysPermissionRestAPI.queryDepartPermission(departId);
		return matApiResponse;
	}

	/**
	 * 保存部门授权
	 *
	 * @return
	 */
	@RequestMapping(value = "/saveDepartPermission", method = RequestMethod.POST)
    //@RequiresPermissions("system:permission:saveDepart")
	public MatApiResponse<String> saveDepartPermission(@RequestBody JSONObject json) {
		MatApiResponse<String> matApiResponse = sysPermissionRestAPI.saveDepartPermission(json);
		return matApiResponse;
	}

}
